package com.fulihui.usercore.biz.conv;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.near.toolkit.common.EnumUtil;
import org.springframework.util.CollectionUtils;

import com.fulihui.usercore.dal.dataobj.TagDO;
import com.fulihui.usercore.dto.TagDTO;
import com.fulihui.usercore.enums.CommonStateEnum;

/**
 *
 * Created by Willard.Hu on 2016/5/26.
 */
public class TagConv {

    public static TagDO toDO(TagDTO tagDTO) {
        if (tagDTO == null) {
            return null;
        }
        TagDO tag = new TagDO();
        tag.setId(tagDTO.getId());
        tag.setTagName(tagDTO.getTagName());
        CommonStateEnum state = tagDTO.getState();
        tag.setState(state != null ? state.getCode() : CommonStateEnum.ON.getCode());
        return tag;
    }

    public static TagDTO toDTO(TagDO tagDO) {
        if (tagDO == null) {
            return null;
        }
        TagDTO dto = new TagDTO();
        dto.setId(tagDO.getId());
        dto.setTagName(tagDO.getTagName());
        dto.setGmtCreate(tagDO.getGmtCreate());
        dto.setGmtModified(tagDO.getGmtModified());
        dto.setState(EnumUtil.queryByCode(tagDO.getState(), CommonStateEnum.class));
        return dto;
    }

    public static List<TagDTO> toList(List<TagDO> doList) {
        if (CollectionUtils.isEmpty(doList)) {
            return Collections.emptyList();
        }
        return doList.stream().map(TagConv::toDTO).collect(Collectors.toList());
    }

}
